﻿<Window x:Class="VoiceTestPlatform.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="美的生活电器智能语音测试系统--20170110.01" Height="540" Width="1320" 
        xmlns:l="clr-namespace:VoiceTestPlatform" MinWidth="540"
          xmlns:dd="urn:gong-wpf-dragdrop"
         xmlns:ctrls="clr-namespace:VoiceTestPlatform.Controls">
    <Window.Resources>
        <DataTemplate x:Key="DragAdorner">
            <Border Background="Silver"
              BorderBrush="Gray"
              BorderThickness="1"
              CornerRadius="2"
              SnapsToDevicePixels="True"
              Margin="1">
                <StackPanel Orientation="Horizontal">
                    <!--<Image Source="/icon.png"
                 Width="16"
                 Margin="0,0,2,0"
                 VerticalAlignment="Center" />-->
                    <TextBlock Text="{Binding}"
                     Margin="0,0,2,0"
                     VerticalAlignment="Center" />
                </StackPanel>
            </Border>
        </DataTemplate>
    </Window.Resources>
    <Grid>
        <Grid.Resources>

            <l:StopBitsToStrConverter x:Key="sbcon" />
            <l:ParityToStrConverter x:Key="pcon" />
            <l:PlayingToBackgroundConverter x:Key="ptobgcon" />
            <l:BoolReverseConveter x:Key="brcon" />
            <Style TargetType="TextBlock" >
                <Setter Property="Margin" Value="0,5,0,0"/>
                <Setter Property="VerticalAlignment" Value="Center"/>
            </Style>
            <Style TargetType="ComboBox" >
                <Setter Property="Margin" Value="10,5,0,0"/>
            </Style>


        </Grid.Resources>
        <Grid.RowDefinitions>
            <RowDefinition Height="auto" />
            <RowDefinition />
            <RowDefinition Height="auto" />
        </Grid.RowDefinitions>

        <!--菜单项-->
        <StackPanel Orientation="Horizontal">
            <StackPanel.Resources>
                <Style TargetType="Button">
                    <Setter Property="Width" Value="60" />
                    <Setter Property="Height" Value="60" />
                    <Setter Property="Margin" Value="5,5,0,5" />
                </Style>
            </StackPanel.Resources>
            <Button Command="{Binding ProjectCommand}">语音库</Button>
            <Button Command="{Binding RecordCommand}">
                <TextBlock Width="40" TextWrapping="WrapWithOverflow" Margin="0">
                    误操作测试
                </TextBlock>
            </Button>
            <Button Command="{Binding PortTestCommand}" Visibility="Collapsed">
                <TextBlock Width="40" TextWrapping="WrapWithOverflow" Margin="0">
                    模拟串口测试
                </TextBlock>
            </Button>
            <Button Command="{x:Static l:SoundManager.StartRecordCommand}" Visibility="Collapsed">
                <TextBlock Width="45" TextWrapping="WrapWithOverflow" Margin="0">
                    开始录音(测试)
                </TextBlock>
            </Button>
            <Button Command="{x:Static l:SoundManager.StopRecordCommand}" Visibility="Collapsed">
                <TextBlock Width="45" TextWrapping="WrapWithOverflow" Margin="0">
                    结束录音(测试)
                </TextBlock>
            </Button>

            <Button Command="{Binding ArtificialCommand}">
                <TextBlock Width="40" TextWrapping="WrapWithOverflow" Margin="0">
                    人工测试
                </TextBlock>
            </Button>

            <Button Command="{Binding NetworkStatusCommand}">
                <TextBlock Width="40" TextWrapping="WrapWithOverflow" Margin="0">
                    网络状态记录
                </TextBlock>
            </Button>
            <Button Click="PortSetting_Click">
                <TextBlock Width="40"  TextWrapping="WrapWithOverflow" Margin="0">
                    串口设置开关
                </TextBlock>
            </Button>

            <Button Command="{Binding ParameterCommand}">
                <TextBlock Width="40"  TextWrapping="WrapWithOverflow" Margin="0">
                    其它参数设置
                </TextBlock>
            </Button>
            
            <TextBlock Text="{Binding ModeText}" FontSize="12" Foreground="Red" Margin="8,0,8,0" ></TextBlock>

            <!--距离 角度 噪音背景 时间差-->
            <TextBlock Style="{StaticResource menuTxt}">距离:</TextBlock>
            <TextBox Style="{StaticResource menuTxtInput}" Text="{Binding DistanceText, Source={StaticResource  pm},UpdateSourceTrigger=PropertyChanged}"></TextBox>
            <TextBlock Style="{StaticResource menuTxt}">角度:</TextBlock>
            <TextBox Style="{StaticResource menuTxtInput}" Text="{Binding AngelText,Source={StaticResource  pm},UpdateSourceTrigger=PropertyChanged}"></TextBox>
            <TextBlock Style="{StaticResource menuTxt}">噪音背景:</TextBlock>
            <TextBox Style="{StaticResource menuTxtInput}" Text="{Binding NoiseText,Source={StaticResource  pm},UpdateSourceTrigger=PropertyChanged}"></TextBox>
        </StackPanel>

        <Grid  Grid.Row="1" >
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="auto" />
                <ColumnDefinition  />
                <ColumnDefinition Width="auto" />
                <ColumnDefinition Width="auto" />
            </Grid.ColumnDefinitions>

            <GroupBox Header="串口设置" Width="240" HorizontalAlignment="Left" Name="PortSetting" >
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="80" />
                        <ColumnDefinition  />
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="auto"  />
                        <RowDefinition Height="auto" />
                        <RowDefinition Height="auto" />
                        <RowDefinition Height="auto" />
                        <RowDefinition Height="auto" />
                        <RowDefinition Height="auto" />
                        <RowDefinition Height="auto" />
                    </Grid.RowDefinitions>
                    <TextBlock Text="串口号" TextAlignment="Right" ></TextBlock>
                    <ComboBox Grid.Column="1" IsEnabled="{Binding IsPortOpening,Converter={StaticResource brcon}}" ItemsSource="{Binding SerialPorts,UpdateSourceTrigger=PropertyChanged}" SelectedItem="{Binding SelectedPort}" />


                    <TextBlock Text="波特率" TextAlignment="Right" Grid.Row="1" ></TextBlock>
                    <ComboBox Grid.Column="1" Grid.Row="1" IsEnabled="{Binding IsPortOpening,Converter={StaticResource brcon}}"
                              ItemsSource="{Binding BaudRateSource}" SelectedItem="{Binding SelectedBaudRate}" />


                    <TextBlock Text="停止位" TextAlignment="Right" Grid.Row="2" ></TextBlock>
                    <ComboBox Grid.Column="1" Grid.Row="2" IsEnabled="{Binding IsPortOpening,Converter={StaticResource brcon}}"
                              ItemsSource="{Binding StopBitsSource}" SelectedItem="{Binding SelectedStopBit}">
                        <ComboBox.ItemTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding Converter={StaticResource  sbcon},Mode=OneWay}" />
                            </DataTemplate>
                        </ComboBox.ItemTemplate>
                    </ComboBox>

                    <TextBlock Text="奇偶校验" TextAlignment="Right" Grid.Row="3" ></TextBlock>
                    <ComboBox Grid.Column="1" IsEnabled="{Binding IsPortOpening,Converter={StaticResource brcon}}"
                              Grid.Row="3" ItemsSource="{Binding ParitySource }" SelectedItem="{Binding SelectedParity}">
                        <ComboBox.ItemTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding Converter={StaticResource  pcon},Mode=OneWay}" />
                            </DataTemplate>
                        </ComboBox.ItemTemplate>
                    </ComboBox>

                    <TextBlock Text="数据位" TextAlignment="Right" Grid.Row="4" ></TextBlock>
                    <ComboBox Grid.Column="1" Grid.Row="4" IsEnabled="{Binding IsPortOpening,Converter={StaticResource brcon}}"
                              ItemsSource="{Binding DataBitSource }" SelectedItem="{Binding SelectedDataBit}">
                    </ComboBox>

                    <TextBlock Text="编码" TextAlignment="Right" Grid.Row="5" ></TextBlock>
                    <ComboBox Grid.Column="1" Grid.Row="5" IsEnabled="{Binding IsPortOpening,Converter={StaticResource brcon}}"
                              ItemsSource="{Binding EncodingSource}" SelectedItem="{Binding SelectedEncoding}" />

                    <StackPanel Grid.Row="6" Orientation="Horizontal" Grid.ColumnSpan="2" Margin="0,12,0,0" HorizontalAlignment="Center">
                        <Button Command="{Binding OpenSerialPortCommand}" IsEnabled="{Binding IsPortOpening,Converter={StaticResource brcon}}"
                                Style="{StaticResource FooterButton}">打开串口</Button>
                        <Button Command="{Binding CloseSerialPortCommand}" Style="{StaticResource FooterButton}">关闭串口</Button>
                        <Button Command="{Binding RefreshSerialPortCommand}" Style="{StaticResource FooterButton}">刷新串口</Button>
                    </StackPanel>

                    <GroupBox Header="模拟发送串口" Grid.ColumnSpan="2" Grid.Row="7" Margin="0,8,0,0"
                              Visibility="{Binding PortTestVisibility}">
                        <StackPanel>
                            <StackPanel Orientation="Horizontal">
                                <TextBlock Text="串口号" TextAlignment="Right" Width="80"  ></TextBlock>
                                <ComboBox  IsEnabled="{Binding IsSendPortOpening,Converter={StaticResource brcon}}" ItemsSource="{Binding SerialPorts}" SelectedItem="{Binding SelectedSendPort}" />
                            </StackPanel>
                            <TextBlock Margin="8,8,0,0" Text="{Binding SendOpenText,StringFormat=是否已打开: {0}}"  />
                            <TextBox TextWrapping="Wrap" AcceptsReturn="True" Margin="8,20,0,0" Height="56"
                                    Text="{Binding SendText}" />
                            <StackPanel Orientation="Horizontal" HorizontalAlignment="Left" Margin="8,8,0,0">
                                <Button Command="{Binding SendCommand}" Style="{StaticResource FooterButton}" >发送</Button>
                                <Button Command="{Binding OpenSendCommand}" Style="{StaticResource FooterButton}" 
                                        IsEnabled="{Binding IsSendPortOpening,Converter={StaticResource brcon}}" >打开</Button>
                                <Button Command="{Binding CloseSendCommand}" Style="{StaticResource FooterButton}" >关闭</Button>
                            </StackPanel>
                        </StackPanel>
                    </GroupBox>
                </Grid>
            </GroupBox>
            <GroupBox Grid.Column="1" Header="接收数据">


                <TabControl >
                    <TabItem Header="原消息">
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="63*"/>
                                <ColumnDefinition Width="353*"/>
                            </Grid.ColumnDefinitions>
                            <Grid.RowDefinitions>
                                <RowDefinition />
                                <RowDefinition Height="auto" />
                            </Grid.RowDefinitions>
                            <TextBox TextWrapping="Wrap" x:Name="txtRev" AcceptsReturn="True" UndoLimit="1" Text="{Binding ReceiveText}" ScrollViewer.VerticalScrollBarVisibility="Visible" IsReadOnly="True" Grid.ColumnSpan="2"></TextBox>
                            <StackPanel Orientation="Horizontal" Grid.Row="1" Grid.ColumnSpan="2">
                                <Button Style="{StaticResource FooterButton}" Command="{Binding ClearTextCommand}">清空</Button>
                                <Button Style="{StaticResource FooterButton}" Command="{Binding SaveTextCommand}">保存</Button>

                            </StackPanel>
                        </Grid>
                    </TabItem>
                    <TabItem Header="结果">
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition />
                                <RowDefinition Height="auto" />
                            </Grid.RowDefinitions>
                            <ListView ItemsSource="{x:Static l:ResultManager.ResultItems}" ScrollViewer.HorizontalScrollBarVisibility="Auto"
                                      ScrollViewer.VerticalScrollBarVisibility="Auto">
                                <ListView.View>
                                    <GridView AllowsColumnReorder="true" ColumnHeaderToolTip="结果视图">
                                        <GridViewColumn DisplayMemberBinding= "{Binding Path=TestDate}"   Header="时间" Width="100"/>
                                        <GridViewColumn DisplayMemberBinding= "{Binding Path=ResultText}" Header="结果" Width="100" ToolTipService.ToolTip="heee"/>
                                        <GridViewColumn DisplayMemberBinding="{Binding Path=Recognition}"  Header="识别" Width="100">
                                        </GridViewColumn>
                                        <GridViewColumn DisplayMemberBinding="{Binding Path=AudioCommand}"  Header="音频命令" Width="100"/>
                                        <GridViewColumn DisplayMemberBinding="{Binding Path=Audio}"  Header="相关音频" Width="100"/>

                                        <GridViewColumn DisplayMemberBinding="{Binding Path=DistanceText}"  Header="距离" Width="100"/>
                                        <GridViewColumn DisplayMemberBinding="{Binding Path=AngelText}"  Header="角度" Width="100"/>
                                        <GridViewColumn DisplayMemberBinding="{Binding Path=NoiseText}"  Header="噪音背景" Width="100"/>
                                        <GridViewColumn DisplayMemberBinding="{Binding Path=RecognitionTimeString}"  Header="识别用时" Width="100"/>

                                    </GridView>
                                </ListView.View>
                            </ListView>
                            <StackPanel Orientation="Horizontal" Grid.Row="1">
                                <Button Style="{StaticResource FooterButton}" Command="{Binding ResultToExcelCommand}">导出Excel</Button>
                                <Button Style="{StaticResource FooterButton}" Command="{Binding ClearResultCommand}">清空结果</Button>
                                <Button Style="{StaticResource FooterButton}" Command="{Binding SatResultCommand}" Width="100" >导出统计Excel</Button>
                            </StackPanel>
                        </Grid>
                    </TabItem>
                </TabControl>
            </GroupBox>

            <GroupBox Grid.Column="2" Header="语音库">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="auto" />
                        <RowDefinition Height="auto" />
                        <RowDefinition />
                    </Grid.RowDefinitions>
                    <StackPanel Orientation="Horizontal" Margin="8,8,0,0" >
                        <TextBlock Text="关键字:" VerticalAlignment="Center"  />
                        <TextBox Margin="8,0"  VerticalAlignment="Center"  Width="150" Text="{Binding Keyword,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}"/>
                    </StackPanel>
                    <StackPanel Grid.Row="1" Orientation="Horizontal" Margin="8,8,0,0" MaxWidth="320" HorizontalAlignment="Left">
                        <TextBlock Text="路径:" VerticalAlignment="Center" />
                        <TextBlock Text="{Binding LibPath}" VerticalAlignment="Center"  TextWrapping="Wrap" />
                    </StackPanel>
                    <ListBox Grid.Row="2" ItemsSource="{Binding SoundsSource}" Width="340" Margin="0,13,0,8" MouseDoubleClick="ListBox_MouseDoubleClick">
                        <ListBox.ItemTemplate>
                            <DataTemplate>
                                <StackPanel Orientation="Horizontal"  >
                                    <TextBlock Text="{Binding DisplayText}" Width="180" ToolTip="{Binding DisplayText}"  TextTrimming="CharacterEllipsis"></TextBlock>
                                    <Button Margin="5,0,0,0" Width="80" Command="{x:Static l:ProjectManager.AddProjectCommand}" CommandParameter="{Binding}"  >添加到流程</Button>
                                    <Button Margin="5,0,0,0" Width="55" Command="{x:Static l:SoundManager.PlayCommand}" CommandParameter="{Binding}" >试听</Button>
                                </StackPanel>
                            </DataTemplate>
                        </ListBox.ItemTemplate>

                    </ListBox>
                </Grid>
            </GroupBox>

            <GroupBox Grid.Column="3" Header="播放流程">
                <GroupBox.Resources>
                    <l:ProjectManager x:Key="ProjectManager"/>
                </GroupBox.Resources>
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="auto" />
                        <RowDefinition Height="auto" />
                        <RowDefinition Height="auto" />
                        <RowDefinition />
                    </Grid.RowDefinitions>
                    <StackPanel Orientation="Horizontal">
                        <Button Style="{StaticResource FooterButton}" IsEnabled="{Binding IsPlayProjectEnabled}" Command="{Binding PlayProjectCommand}">播放</Button>
                        <Button Style="{StaticResource FooterButton}" IsEnabled="{Binding IsStopProjectEnabled}" Command="{Binding StopProjectCommand}" >停止</Button>
                        <Button Style="{StaticResource FooterButton}" Command="{x:Static l:ProjectManager.LoadProjectCommand}" >加载</Button>
                        <Button Style="{StaticResource FooterButton}" Command="{x:Static l:ProjectManager.SaveProjectCommand}" >保存</Button>
                    </StackPanel>
                    <StackPanel Orientation="Horizontal" Grid.Row="1">
                        <Button Style="{StaticResource FooterButton}"  Command="{Binding ClearProjectCommand}">清除</Button>
                        <Button Style="{StaticResource FooterButton}" IsEnabled="{Binding IsPauseProjectEnabled}" Command="{Binding PauseProjectCommand}" >暂停</Button>
                    </StackPanel>
                    <StackPanel Grid.Row="2" Orientation="Horizontal">
                        <TextBlock Text="播放次数:" VerticalAlignment="Center"  />
                        <TextBox Margin="8,0"  VerticalAlignment="Center"  Width="70" Text="{Binding LoopTimes,UpdateSourceTrigger=PropertyChanged}"/>
                        <TextBlock Text="剩余次数:" VerticalAlignment="Center"  />
                        <TextBlock  VerticalAlignment="Center"  Text="{Binding LeftPlayTimes}" />
                    </StackPanel>
                    <!--<ListBox ItemsSource="{Binding Source={StaticResource ProjectManager}, Path=ProjectItems}" Grid.Row="1" Margin="5,8" Width="260">-->
                    <Border Margin="5,8" Width="260"  Grid.Row="3" BorderBrush="Silver" BorderThickness="1">
                        <ScrollViewer  VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto" Margin="2"
                                         >
                            <!--Drop="ItemsControl_Drop"
                                          AllowDrop="True"-->
                            <!--dd:DragDrop.IsDragSource="True"
                      dd:DragDrop.IsDropTarget="True"
                      dd:DragDrop.DragAdornerTemplate="{StaticResource DragAdorner}"-->
                            <ItemsControl Background="Silver" x:Name="ic" ItemsSource="{x:Static l:ProjectManager.ProjectItems}"  MouseDoubleClick="ic_MouseDoubleClick"                                    
                                         
                                          dd:DragDrop.IsDragSource="True"
                      dd:DragDrop.IsDropTarget="True"
                                          dd:DragDrop.DropHandler="{Binding}"
                      dd:DragDrop.DragAdornerTemplate="{StaticResource DragAdorner}"
                                        >
                                <ItemsControl.ItemTemplate>
                                    <DataTemplate>
                                        <Border BorderBrush="LightBlue" BorderThickness="1"  Margin="0,2,0,0" Background="{Binding IsPlaying,Converter={StaticResource ptobgcon}}">
                                            <Border.ContextMenu>
                                                <ContextMenu>
                                                    <MenuItem Header="试听" Command="{x:Static l:SoundManager.PlayCommand}" CommandParameter="{Binding}" />
                                                    <MenuItem Header="删除" Command="{x:Static l:ProjectManager.DelProjectItemCommand}" CommandParameter="{Binding}" />
                                                </ContextMenu>
                                            </Border.ContextMenu>
                                            <StackPanel  Margin="4" >
                                                <TextBlock Width="240" Text="{Binding DisplayText}"  ToolTip="{Binding DisplayText}"  TextTrimming="CharacterEllipsis" />
                                                <!--<TextBlock Margin="12,4,4,4" Text="{Binding PlayTimes, StringFormat=播放次数:  {0} }" />
                                                <TextBlock Margin="12,4,4,4" Text="{Binding LoopText, StringFormat=是否循环:  {0} }" />-->
                                                <!--<StackPanel  Orientation="Horizontal">
                                                    <Button Style="{StaticResource FooterButton}" Command="{x:Static l:SoundManager.PlayCommand}" CommandParameter="{Binding}" >试听</Button>
                                                   
                                                    <Button Style="{StaticResource FooterButton}" Command="{x:Static l:ProjectManager.DelProjectItemCommand}" CommandParameter="{Binding}">删除</Button>
                                                </StackPanel>-->
                                                
                                            </StackPanel>
                                        </Border>

                                    </DataTemplate>
                                </ItemsControl.ItemTemplate>
                            </ItemsControl>
                        </ScrollViewer>
                    </Border>
                </Grid>

            </GroupBox>

        </Grid>

        <!--footer-->
        <StackPanel Grid.Row="2" Orientation="Horizontal" Visibility="Collapsed">
            <Button Command="{Binding ExcelTestCommand}">Excel测试</Button>
            <Button Command="{Binding SoundTestCommand}">声音测试</Button>
        </StackPanel>
    </Grid>
</Window>
